package service

import (
	"github.com/go-playground/validator/v10"

	"forum/pkg/domain"
)

type threadService struct {
	threadRepo domain.ThreadRepository
}

func NewThreadService(threadRepo domain.ThreadRepository) domain.ThreadService {
	return &threadService{
		threadRepo: threadRepo,
	}
}

func (svc *threadService) InsertThread(thread *domain.Thread) error {
	_, err := svc.threadRepo.GetThreadByTopic(thread.Topic)
	if err != nil {
		return err
	}

	if err := svc.checkThread(thread); err != nil {
		return err
	}

	return svc.threadRepo.CreateThread(thread)
}

func (svc *threadService) checkThread(thread *domain.Thread) error {
	// realize this
	validate := validator.New()

	if err := validate.Struct(thread); err != nil {
		return err
	}

	return nil
}
